package pathways.Applet;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.UIManager;

import pathways.CEnzymeMouseListener;
import pathways.CEnzymePainter;
import pathways.CMain;
import pathways.CPathway;
import pathways.CPathwayPainter;
import pathways.CReaction;
import pathways.CSubstrateMouseListener;
import pathways.CSubstratePainter;
import pathways.Database.Implementation.CDBConnection;
import pathways.Database.Implementation.CDatabase;
import pathways.Database.Specialized.Visual.CDBSVSearchReaction;
import pathways.util.Debug.CDebug;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

public class CAppletInterface extends JApplet
{
  boolean isStandalone = false;
  private static URL url = null;
  JButton btnLaunch = new JButton();

  public static URL getBase()
  {
    return url;
  }

  public static URL getMolFileFolder()
  {
    try {
        return new URL(url.toString() + "molfiles/");
    } catch (MalformedURLException mue) {
      CDebug.println("static","getMoleFileFolder() Bad url. Exception: " + mue.toString());
      return null;
    }
  }

  /**Get a parameter value*/
  public String getParameter(String key, String def)
  {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public CAppletInterface()
  {
  }
  /**Initialize the applet*/
  public void init()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

    url = this.getCodeBase();
    System.out.println(url.toString());
  }
  /**Component initialization*/
  private void jbInit() throws Exception
  {
    btnLaunch.setText("Launch");
    btnLaunch.setBounds(new Rectangle(53, 25, 187, 26));

    btnLaunch.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnLaunch_actionPerformed(e);
      }
    });

    this.getContentPane().setLayout(null);
    this.setSize(new Dimension(312, 74));
    this.getContentPane().add(btnLaunch, null);

  }
  /**Start the applet*/
  public void start()
  {
  }
  /**Stop the applet*/
  public void stop()
  {
  }
  /**Destroy the applet*/
  public void destroy()
  {
  }
  /**Get Applet information*/
  public String getAppletInfo()
  {
    return "Applet Information";
  }
  /**Get parameter info*/
  public String[][] getParameterInfo()
  {
    return null;
  }

  //static initializer for setting look & feel
  static
  {
    try
    {
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch(Exception e)
    {
    }
  }

  void btnLaunch_actionPerformed(ActionEvent e)
  {
    CDBConnection conn = new CDBConnection();

    try {
        conn.setClass("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        conn.setConnect("jdbc:microsoft:sqlserver://pathways.stjohns.edu:1433;user=sa;password=pathways951357;databasename=pathways1;SelectMethod=cursor;");
    } catch (Exception excep)
    {
      System.out.println("Unable to establish connection to database. Exception: " + excep.toString());
    }

    CDatabase db = new CDatabase(conn);

    CPathway p = new CPathway(db);

    CSubstratePainter subPainter = new CSubstratePainter();
    CEnzymePainter enzymePainter = new CEnzymePainter();
    CPathwayPainter pathwayPainter = new CPathwayPainter(subPainter,enzymePainter);
    p.setPainter(pathwayPainter);

    CSubstrateMouseListener listener = new CSubstrateMouseListener(p);
    p.setSubstrateMouseListener(listener);
    CEnzymeMouseListener enzymeListener = new CEnzymeMouseListener(p);
    p.setEnzymeMouseListener(enzymeListener);

    Rectangle rect = new Rectangle(10,300,100,50);
    Rectangle rect2 = new Rectangle(200,300,100,50);

    CMain main = new CMain(p,db);

    CDBSVSearchReaction c = new CDBSVSearchReaction(db,main.getFrames()[0],rect,rect2,2,0);
    c.show();

    CReaction rxn = c.getReaction();

    if (rxn != null)
    {


      //CSubstrate sub = new CSubstrate("Compound1",0,rect);
      //CEnzyme enzyme = new CEnzyme("Compound2",0);
      //CSubstrate prod = new CSubstrate("Compound3",1,rect2);

      //CReaction rxn = new CReaction(sub,enzyme,prod,2,0,0);

      p.addReaction(rxn);

    }

    main.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){System.exit(0);}});
    main.show();

    p.draw();
  }
}
